import 'babel-polyfill'
import React from 'react';
import ReactDOM from 'react-dom';
import './common/common.scss'
import registerServiceWorker from './registerServiceWorker';
import Routers from './router'
import message from 'antd/lib/message'
import axios from 'axios'

axios.interceptors.response.use(function (response) {
    const { data, data: { code, desc } } = response;
    //请求成功
    if (code === '1') {
        return data
    }
    // token 已过期，重定向到登录页面  
    else if (code === '2001') {
        sessionStorage.removeItem('isLogin')
        message.error('用户未登录')
        setTimeout(() => {
            document.location.pathname = '/login'
        }, 500)
        return false
    }
    // 登陆需要验证码陆需要验证码
    else if (code === '2002') {
        return data
    }
    //验证码相关
    else if (code === '2003') {
        return data
    }
    //请求失败
    else {
        message.error(desc)
        return false
    }

}, function (error) {
    if (error.response.status === 500) {
        message.error('网络连接错误，请检查网络再试')
    }
    console.log(error)
})

ReactDOM.render(<Routers />, document.getElementById('root'));
registerServiceWorker();
